package com.wanmait.spacesys.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.spacesys.dao.UserinfoDAO;
import com.wanmait.spacesys.util.CookieUtil;
import com.wanmait.spacesys.vo.Userinfo;

@WebServlet("/UserinfoController")
public class UserinfoController extends BaseController{
	UserinfoDAO userinfoDAO = new UserinfoDAO();

	//用户注册
	protected void registe(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String nickname =  request.getParameter("nickname");
		String regUsername = request.getParameter("regUsername");
		String regUserpass = request.getParameter("regUserpass");
		Userinfo userinfo = new Userinfo();
		userinfo.setNickname(nickname);
		userinfo.setUsername(regUsername);
		userinfo.setPassword(regUserpass);
		
		userinfoDAO.insert(userinfo);
		response.sendRedirect(request.getContextPath()+"/web/userLogin.jsp?mes=success");
	}
	
	//用ajax判断账号是否重复
	protected void isSame(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String regUsername =  request.getParameter("regUsername");
		int result =  userinfoDAO.isExist(regUsername);
		PrintWriter out = response.getWriter();
		if(result>0) {
			out.print("have");
		}
		else{
			out.print("nothave");
		}
	}
	
	//用户登录ajax判断账号密码是否正确
	protected void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("userpass");
		Userinfo userinfo =  userinfoDAO.isRight(username, password);
		PrintWriter out = response.getWriter();
		//判断用户是否被拉黑
		if(userinfo==null) {
			out.print("fail");
		}
		else {
			if(userinfo.isEnable()==false){
				out.print("block");
			}
			else {
				out.print("ok");
			}
		}
//		if(userinfo!=null&&userinfo.isEnable()==false) {
//			out.print("block");
//		} 
//		
//		else if(userinfo!=null&&userinfo.isEnable()==true){
//			out.print("ok");
//		}
//		else {
//			//失败
//			out.print("fail");
//		}
		
	}

	//登录表单提交
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username =  request.getParameter("username");
		String password = request.getParameter("password");
		String remember = request.getParameter("remember");
		Userinfo userinfo =  userinfoDAO.isRight(username, password);
		if(userinfo!=null) {
			request.getSession().setAttribute("userinfo", userinfo);
			if("on".equals(remember)) {
				CookieUtil.addCookie("username", username, request.getContextPath(), 60*60*24*365, response);
				CookieUtil.addCookie("password", password, request.getContextPath(), 60*60*24*365, response);
//				//登录成功后跳转到上一次访问的页面
//				 if(url.contains(request.getContextPath()+"/web/userLogin.jsp")) {
//					 response.sendRedirect(request.getContextPath()+"/web/index.jsp");
//				 }
//				 else {
//					 response.sendRedirect(referer);
//				 }
//				response.sendRedirect(request.getContextPath()+"/web/index.jsp");
			}
			else {
				CookieUtil.addCookie("username", username, request.getContextPath(), 0, response);
				CookieUtil.addCookie("password", password, request.getContextPath(), 0, response);
//				 if(url.equals(request.getContextPath()+"/web/userLogin.jsp")) {
////					 response.sendRedirect(request.getContextPath()+"/web/index.jsp");
//					 if(url.contains("userLogin.jsp")) {
//						 response.sendRedirect(request.getContextPath()+"/web/index.jsp");
//					 }
//					 else {
//						 response.sendRedirect(url);
//					 }
//				 }
//				 else {
//					 response.sendRedirect(url);
//				 }

			}
			 String oldPath = request.getQueryString();
	            int start = oldPath.indexOf("old=");
	            String prev = oldPath.substring(start + 4);
	            if (prev.contains("userLogin.jsp") || prev.contains("userRegister.jsp")||prev.contains("retrievePass.jsp")||prev.contains("forgetPass.jsp")||prev.contains("changeuserpass.jsp")) {
	                response.sendRedirect(request.getContextPath() + "/web/index.jsp");
	            } else {
	                response.sendRedirect(prev);
	            }
//			if(url.contains("userLogin.jsp")) {
//				response.sendRedirect(request.getContextPath()+"/web/index.jsp");
//				
//			}
			//从userLogin.jsp过来的 referer=<%=request.getHeader("referer")
//			 if(referer.contains("userRegister.jsp")) {//唯一的注册
//				response.sendRedirect(request.getContextPath()+"/web/index.jsp");
//				return;
//			}
//			else {
//				if(referer==null) {
//					response.sendRedirect(request.getContextPath()+"/web/index.jsp");
//				}
//				else {
//					referer=referer+"&id=10";
//					System.out.println(referer);
//					response.sendRedirect(referer);
//				}
//			}
				
		}
		
	}
	
	//退出登录 清除cookie
	protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession().removeAttribute("userinfo");
		Cookie userCookie = new Cookie("username", null);
		userCookie.setPath(request.getContextPath());
		userCookie.setMaxAge(0);
		Cookie passCookie = new Cookie("password",null);
		passCookie.setPath(request.getContextPath());
		passCookie.setMaxAge(0);
		response.addCookie(passCookie);
		String thisPath = request.getHeader("Referer");
        if (thisPath.contains("collection.jsp")) {
            response.sendRedirect(request.getContextPath() + "/web/index.jsp");
        } else {
            response.sendRedirect(thisPath);
        }
		//从哪个页面跳转过来的？
//		String referer =  request.getParameter("referer");
		//当前页面的路径。
//		String url = request.getParameter("url");
//		if(url.contains("index.jsp")) {
//			response.sendRedirect(url);
//		}
//		if(url.contains("collection.jsp")) {
//			response.sendRedirect(request.getContextPath()+"/web/index.jsp");
//		}
//
	}
	
	@Override
	protected void doDefault(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

}
